﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class NhaCungCapBUO:IBUO<tbl_NhaCungCap>
    {
        CSDLBanHangDataContext context = new CSDLBanHangDataContext();
        public bool Insert(tbl_NhaCungCap obj)
        {

            try
            {
                context.tbl_NhaCungCaps.InsertOnSubmit(obj);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        /// <summary>
        /// Xóa nhà cung cấp
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(String id)
        {
            try
            {
                tbl_NhaCungCap obj = context.tbl_NhaCungCaps.Where(n => n.id_NhaCungCap.Equals(id)).ToList<tbl_NhaCungCap>().FirstOrDefault();
                if (obj!=null)
                {
                    context.tbl_NhaCungCaps.DeleteOnSubmit(obj);
                    context.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {

                return false;
            }
        }

        public bool Delete(tbl_NhaCungCap obj)
        {
            throw new NotImplementedException();
        }

        public List<tbl_NhaCungCap> GetAll()
        {
            return context.tbl_NhaCungCaps.OrderByDescending(n => n.id_NhaCungCap).ToList<tbl_NhaCungCap>();
        }

        public tbl_NhaCungCap GetSingle(String id)
        {
            return context.tbl_NhaCungCaps.Where(n=>n.id_NhaCungCap.Trim().Equals(id.Trim())).ToList<tbl_NhaCungCap>().FirstOrDefault();
        }

        public bool Update(String id, tbl_NhaCungCap obj)
        {
            try
            {
                tbl_NhaCungCap tblNCC = context.tbl_NhaCungCaps.Where(n => n.id_NhaCungCap.Equals(id)).ToList<tbl_NhaCungCap>().FirstOrDefault();
                tblNCC = obj;
                context.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        /// <summary>
        /// Tìm theo tên nhà cung cấp
        /// </summary>
        /// <param name="ncc"></param>
        /// <returns></returns>
        public IQueryable SearchByTenNCC(String ncc)
        {
            var list=context.tbl_NhaCungCaps.Where(n=>n.TenNhaCungCap.Contains(ncc));
            return list;
        }

        /// <summary>
        /// Tìm theo tên nhà cung cấp
        /// </summary>
        /// <param name="ncc"></param>
        /// <returns></returns>
        public bool SearchByTenNhaCungCap(String ncc)
        {
            int kt = 0;
            var list = context.tbl_NhaCungCaps.Where(n => n.TenNhaCungCap.Trim().Equals(ncc.Trim())).ToList();
            kt = list.Count;
            if (kt==0)
            {
                return false;
            }
            else
	        {
                return true;
	        }

        }
        /// <summary>
        /// Tìm theo mã số nhà cung cấp
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable SearchByMaNCC(String id)
        {
            var list = context.tbl_NhaCungCaps.Where(n => n.id_NhaCungCap.Contains(id));
            return list;
        }
    }
}
